<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Execute a SELECT command: GNOME Data Access 5 manual</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="GNOME Data Access 5 manual">
<link rel="up" href="howto.html" title="HOWTO for common tasks">
<link rel="prev" href="howto-sqlbuilder.html" title="Build statements without using a parser">
<link rel="next" href="howto-modify-select.html" title="Modify the result of a SELECT command">
<meta name="generator" content="GTK-Doc V1.32 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="howto.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="howto-sqlbuilder.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="howto-modify-select.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="sect1">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="howto-exec-select"></a>Execute a SELECT command</h2></div></div></div>
<p>
      Any SQL command in <span class="application">Libgda</span> must be converted to a <a class="link" href="GdaStatement.html" title="GdaStatement">GdaStatement</a> object
      which is then executed; if the statement defines some variable (place holders), then variables binding
      (assigning values to variables) is done at execution time. Each database provider (database 
      <span class="emphasis"><em>driver</em></span> or <span class="emphasis"><em>adapter</em></span>) can implement its own parser to 
      be able to parse its specific SQL dialect. Except for binding variables, and for reusability, the
      <a class="link" href="GdaStatement.html" title="GdaStatement">GdaStatement</a> object guarantees that it contains only one SQL statement
      (it's not possible for it to contain several ones separated by a semicolon).
    </p>
<p>
      Parsing an SQL statement into a <a class="link" href="GdaStatement.html" title="GdaStatement">GdaStatement</a> object is the job of an
      SqlParser.
    </p>
<p>
      However when one only wants to run a simple SQL statement, <span class="application">Libgda</span> provides the
      gda_connection_statement_execute_select_command() function which does
      everything in one step. The following codes illustrates parsing and executing a SELECT statement:
    </p>
<p>
      </p>
<pre class="programlisting">
GdaSqlParser *parser;
GdaStatement *stmt;
GError *error = NULL;

parser = gda_connection_create_parser (cnc);
if (!parser) {
    /* the database provider used by cnc does not implement its own parser,
     * let's use a generic one */
    parser = gda_sql_parser_new ();
stmt = gda_sql_parser_parse_string (parser, "SELECT * FROM customers", NULL, &amp;error);
g_object_unref (parser);
if (!stmt) {
    /* there was an error while parsing */
}
else {
    GdaDataModel *model;
    model = gda_connection_statement_execute_select (cnc, stmt, NULL, &amp;error);
    if (model) {
        /* dump to results to STDOUT */
        gda_data_model_dump (model, stdout);
        g_object_unref (model);
    }
    else {
        /* there was an error while executing the statement */
    }
}
g_object_unref (stmt);
      </pre>
<p>
    </p>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.32</div>
</body>
</html>